package com.ruoyi.eqmonitor.version.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.eqmonitor.version.domain.BizAppVersion;
import com.ruoyi.eqmonitor.version.mapper.BizAppVersionMapper;
import com.ruoyi.eqmonitor.version.service.IBizAppVersionService;

import breeze.framework.reflect.Reflector;

/**
 * App版本更新Service业务层处理
 * 
 * @author breeze
 * @date 2023-06-21
 */
@Service
public class BizAppVersionServiceImpl extends ServiceImpl<BizAppVersionMapper,BizAppVersion> implements IBizAppVersionService {
    
    @Autowired
    private RemoteFileService remoteFileService;
	
    @Autowired
    private BizAppVersionMapper bizAppVersionMapper;

    @Override
    public BizAppVersion selectBizAppVersionById(Long id){
    	BizAppVersion appVersion = bizAppVersionMapper.selectBizAppVersionById(id);
    	setSingleFile("fileid","file",appVersion);
        return appVersion;
    }
      
    @Override
    public List<BizAppVersion> selectBizAppVersionList(BizAppVersion bizAppVersion){
    	List<BizAppVersion> appVersions = bizAppVersionMapper.selectBizAppVersionList(bizAppVersion);
    	for (int i = 0; i < appVersions.size(); i++) {
    		BizAppVersion appVersion = appVersions.get(i);
    		setSingleFile("fileid","file",appVersion);
		}
        return appVersions;
    }
    
	private void setSingleFile(String idProperty,String fileProperty,Object obj) {
		Object propertyValue = Reflector.getProperty(obj, idProperty);
		if(null != propertyValue && !"".equals(propertyValue.toString())) {
			try {
				Long fileId = Long.parseLong(propertyValue.toString());
				Reflector.setProperty(obj, fileProperty, remoteFileService.getInfo(fileId).getData());
			} catch (Exception e) {
			}
		}
		
	}

	@Override
	public BizAppVersion selectLatestBizAppVersion() {
		BizAppVersion appVersion = bizAppVersionMapper.selectLatestBizAppVersion();
    	setSingleFile("fileid","file",appVersion);
		return appVersion;
	}
}
